import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-11-05
 * Time: 14:09
 */

//用数组来自己实现一个栈
public class MyStack {
    public int[] elem;
    public int userSize;

    public MyStack(int k){
        this.elem = new int[k];
    }

    //入栈
    public void push(int val){
        if (elem.length == userSize){
            this.elem = Arrays.copyOf(elem,elem.length*2);
        }
        elem[userSize] = val;
        userSize++;
    }

    //出栈 获取栈顶元素 但是不删除
    public int peek(){
        //判单是否为空
        if (userSize == 0){
            System.out.println("栈为空！");
            return -1;
        }
        return elem[userSize-1];
    }

    //出栈 获取栈顶元素并删除
    public int pop(){
        if (userSize == 0){
            return -1;
        }
        userSize--;
        return elem[userSize];
    }

    //判断栈是否为空
    public boolean empty(){
        return userSize==0;
    }
}
